package com.baidu.manager.modules.sys.service.impl;

import com.baidu.manager.common.Constant;
import com.baidu.manager.common.utils.Query;
import com.baidu.manager.modules.sys.dao.SysMenuDao;
import com.baidu.manager.modules.sys.dao.SysUserDao;
import com.baidu.manager.modules.sys.entity.SysMenu;
import com.baidu.manager.modules.sys.entity.SysUser;
import com.baidu.manager.modules.sys.redis.SysUserRedis;
import com.baidu.manager.modules.sys.service.SysRoleService;
import com.baidu.manager.modules.sys.service.SysUserRoleService;
import com.baidu.manager.modules.sys.service.SysUserService;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author: jql
 * @description: TODO(系统用户管理)
 * @Date: Created by 14:38 2018/4/13
 */
@Service
public class SysUserServiceImpl implements SysUserService{

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserRedis sysUserRedis;

    @Autowired
    private SysMenuDao sysMenuDao;

    @Override
    public List<SysUser> queryList(Query query) {
        return null;
    }

    @Override
    public int queryTotal(Query query) {
        return 0;
    }

    @Override
    public void save(SysUser user) {
        user.setCreateTime(new Date());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(20);
        user.setPassword(new Sha256Hash(user.getPassword(), salt).toHex());
        user.setSalt(salt);
        sysUserDao.save(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
        sysUserRedis.saveOrUpdate(user);
    }

    @Override
    public SysUser findByUserName(String username) {
        return sysUserDao.findByUserName(username);
    }

    @Override
    public SysUser queryByName(String username) {
        return sysUserDao.findByUserName(username);
    }

    @Override
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList;

        //系统管理员，拥有最高权限
        if(userId == Constant.SUPER_ADMIN){
            List<SysMenu> menuList = sysMenuDao.queryList(new HashMap<>());
            permsList = new ArrayList<>(menuList.size());
            for (SysMenu menu: menuList){
                permsList.add(menu.getPerms());
            }
        }else {
            permsList = sysUserDao.queryAllPerms(userId);
        }
        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for (String perms: permsList){
            if (StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }

    @Override
    public List<Long> queryAllMenuId(Long userId) {
        return sysUserDao.queryAllMenuId(userId);
    }

    @Override
    public SysUser queryObject(Long userId) {
        SysUser sysUser = sysUserRedis.get(userId);
        if (sysUser == null){
            sysUser = sysUserDao.queryObject(userId);
            sysUserRedis.saveOrUpdate(sysUser);
        }
        return sysUser;
    }

    @Override
    @Transactional
    public void update(SysUser user) {
        sysUserRedis.delete(user);

        if (StringUtils.isBlank(user.getPassword())){
            user.setPassword(null);
        }else{
            user.setPassword(new Sha256Hash(user.getPassword(), user.getSalt()).toHex());
        }
        sysUserDao.update(user);

        //保存用户与角色关系
        sysUserRoleService.saveOrUpdate(user.getId(), user.getRoleIdList());
    }


}
